<template>
  <div class="app-container">
    <div class="p4">
      <FaSearchBar :show-toggle="false">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">

          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="queryParams.jobName" placeholder="请输入任务名称" clearable style="width: 150px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="任务组名" prop="jobGroup">
            <el-input v-model="queryParams.jobGroup" placeholder="请输入任务组名" clearable style="width: 150px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 150px">
              <el-option v-for="dict in job_log_status" :key="dict.value" :label="dict.label"
                :value="Number(dict.value)" @change="handleQuery" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </FaSearchBar>
      <el-divider border-style="dashed" style="margin:0px 0px 20px 0px" />

      <el-space wrap>
        <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete"
          v.auth="['job:log$remove']">删除</el-button>

        <el-button type="danger" icon="Delete" @click="handleClean" v-auth="['job:log$remove']">清空</el-button>
        <el-button type="warning" icon="Download" @click="handleExport" v.auth="['job:log$export']">导出</el-button>
      </el-space>

      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" stripe
        highlight-current-row border class="mt4" @sort-change="handleSortChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="#" align="center" prop="id" />
        <!-- <el-table-column label="任务ID" align="center" prop="jobId" /> -->
        <el-table-column label="任务名称" prop="jobName" width="150px" />
        <el-table-column label="任务组名" align="center" prop="jobGroup" width="100px" />
        <el-table-column label="Cron表达式" align="center" prop="cron" width="160px" :show-overflow-tooltip="true" />
        <el-table-column label="执行类型" align="center" prop="execType" width="120px">
          <template #default="scope">
            <el-tag type="warning" v-if="scope.row.execType === 'MANUAL'">手动执行</el-tag>
            <el-tag type="success" v-else-if="scope.row.execType === 'AUTO'">自动执行</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="策略" align="center" prop="misfirePolicy" width="100px">
          <template #default="scope">
            <DictTag :dictData="job_misfire_policy" :value="scope.row.misfirePolicy" />
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="100px">
          <template #default="scope">
            <DictTag :dictData="job_log_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="耗时（毫秒）" align="center" prop="costTime" width="120px" />
        <el-table-column label="开始时间" align="center" prop="startTime" width="180px" :sortable="true" />
        <el-table-column label="结束时间" align="center" prop="endTime" width="180px" :sortable="true" />
        <el-table-column label="错误信息" prop="errorMsg" :show-overflow-tooltip="true" width="300px" />
        <el-table-column label="操作" align="center" width="100px" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button size="small" plain type="danger" icon="Delete" @click="handleDelete(scope.row)"
              v.auth="['job:log$remove']">删除
            </el-button>
          </template>
        </el-table-column>

      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="loadData" />
    </div>
  </div>
</template>

<script setup name="SysJobLog">
import apiSysJobLog from "@/api/tool/jobLog";

import useUserStore from '@/store/modules/user'
const { proxy } = getCurrentInstance();
const userStore = useUserStore();
const route = useRoute();

const { job_log_status, job_misfire_policy, job_concurrent } = proxy.useDict('job_log_status', 'job_misfire_policy', 'job_concurrent');


const dataList = ref([]);
const loading = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    jobId: route.query.jobId || route.params.jobId,
    jobName: undefined,
    jobGroup: undefined,
    status: undefined,
    execType: undefined,
  }
});

const { queryParams } = toRefs(data);


/**
 * 加载数据
 */
function loadData() {
  // loading.value = true;
  apiSysJobLog.page(queryParams.value).then(response => {
    dataList.value = response.data.rows;
    total.value = response.data.total;
    // loading.value = false;
  });
}

/**
 * 搜索
 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  loadData();
}

/**
 * 重置查询表单
 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/**
 * 选中操作
 * @param selection 选中数据
 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


/**
 * 删除定时任务执行日志表数据
 * @param row 数据行
 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除定时任务执行日志表编号为"' + ids + '"的数据项？').then(function () {
    return apiSysJobLog.delete(ids);
  }).then(() => {
    loadData();
    proxy.$modal.success("删除成功");
  }).catch(() => { });
}

/** 清空按钮操作 */
function handleClean() {
  proxy.$modal.confirm("是否确认清空定时任务执行日志数据?").then(function () {
    return apiSysJobLog.cleanByJobId(queryParams.value.jobId);
  }).then(() => {
    loadData();
    proxy.$modal.success("清空成功");
  }).catch(() => { });
}
/**
 * 导出Excel
 */
function handleExport() {
  apiSysJobLog.download(queryParams.value, "定时任务执行日志表");
}

function handleSortChange(sort) {
  queryParams.value = { ...queryParams.value, 'orderBy.prop': sort.prop, 'orderBy.order': sort.order };
  loadData();
};
loadData();
</script>
